﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TongTongMall.MallWeb.ApiRepository.Domain;
using TongTongMall.MallWeb.ApiRepository.Domain.Logger;
using TongTongMall.MallWeb.Core.Enums;
using TongTongMall.MallWeb.Core.IApiRepositories;
using TongTongMall.MallWeb.Core.Order.Enum;
using TongTongMall.MallWeb.Core.Order.Models;
using TongTongMall.UI;

namespace TongTongMall.MallWeb.ApiRepository.Repositories
{
    public class ApiGetLogisticsReposiory : IApiGetLogisticsReposiory
    {
        private readonly IApiContext _apiContext;

        public ApiGetLogisticsReposiory(IApiContext _apiContext)
        {
            this._apiContext = _apiContext;
        }

        public async Task<GetLogisticsDto> GetLogisticsAsync(string orderCode, string logisticCode,
            WaybillCompanyEnum waybillCompany, string logisticsUrl)
        {
            var requestJson = JsonConvert.SerializeObject(new
            {
                SystemPlatForm = 0,
                OrderCode = orderCode,
                LogisticCode = logisticCode,
                CompanyCode = waybillCompany.ToString()
            });
            var logisticsDto = new GetLogisticsDto();
            try
            {
                logisticsDto = await _apiContext.PostAsync<GetLogisticsDto>(logisticsUrl, new StringContent(requestJson, Encoding.UTF8, "application/json"));

            }
            catch (Exception ex)
            {
                ApiLogHelper.LogException(ex);
            }

            return logisticsDto;

        }
    }
}
